package com.xia.demo.lock;

import java.util.concurrent.locks.ReentrantLock;

/**
 * ReentrantLock 可重入锁, 同一个线程可以多次获得锁对象
 *
 * @author : xia
 * @date: 2023-05-03 13:58
 */
public class ReentrantLockTest1 {

    private static ReentrantLock lock = new ReentrantLock();

    public static void main(String[] args) {
        // 如果有竞争就进入`阻塞队列`, 一直等待着,不能被打断
        // 主线程main获得锁
        lock.lock();
        try {
            System.out.println(Thread.currentThread().getName()+"锁定……");
            m1();
        } finally {
            lock.unlock();
        }
    }

    private static void m1() {
        lock.lock();
        try {
            System.out.println(Thread.currentThread().getName()+"锁定……");
            m2();
        } finally {
            lock.unlock();
        }
    }

    private static void m2() {
        System.out.println(Thread.currentThread().getName()+"锁定……");
    }
}
